package com.numberone.master.modules.rsa.web;

import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.encryption.RSAUtils;
import com.numberone.master.common.exception.RespResult;
import com.numberone.master.common.menu.RespCode;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.modules.rsa.entity.RsaKey;
import com.numberone.master.modules.rsa.service.RsaService;
import com.numberone.master.modules.sys.utils.UserUtils;

/**
 * rsa密匙Controller
 * @author chengkang
 * @version 2018-01-23
 */
@Controller
@RequestMapping(value = "${adminPath}/rsa")
public class RsaKeyController extends BaseController {
	
	@Autowired
	private RsaService rsaService;
	
	/**
	 * 打开密匙详情页面
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("rsa:rsa:info")
	@RequestMapping(value = "info")
	public String info(Model model, RedirectAttributes redirectAttributes) {
		//根据当前登录人查询
		String id = UserUtils.getUser().getId();
		RsaKey rsaKey = rsaService.selectRsaKey(id);
		model.addAttribute("rsaKey", rsaKey);
		return "modules/sys/rsaKeyInfo";
	}
	
	/**
	 * 修改
	 * @param p
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("rsa:rsa:update")
	@RequestMapping(value = "update")
	@ResponseBody
	public RespResult update(String id, Model model, RedirectAttributes redirectAttributes) {
		RsaKey p =new RsaKey();
		String dlPublicKey = "";//开发者公匙
		String dlPrivateKey = "";//开发者密匙
		String pfPublicKey = "";//平台公匙
		String pfPrivateKey = "";//平台密匙
		p.setCreateDate(new Date());
		p.setUser(UserUtils.getUser());
		//生成app_key密匙
		String appKey = StringUtils.getLetterAndNum(4);
		try {
			//生成开发者的RSA加密参数
			Map<String, Object> developerMap = RSAUtils.genKeyPair();
			dlPublicKey = RSAUtils.getPublicKey(developerMap);
			dlPrivateKey = RSAUtils.getPrivateKey(developerMap);
			//生成平台的RSA的加密参数
			Map<String, Object> platformMap = RSAUtils.genKeyPair();
			pfPublicKey = RSAUtils.getPublicKey(platformMap);
			pfPrivateKey = RSAUtils.getPrivateKey(platformMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		p.setPublicKey(dlPublicKey);
		p.setPrivateKey(pfPrivateKey);
		p.setAppKey(appKey);
		if(StringUtils.isNotBlank(id) == false){
			rsaService.insert(p);
		}else{
			p.setId(id);
			rsaService.update(p);
		}
		p.setPfPublicKey(pfPublicKey);
		p.setDlPrivateKey(dlPrivateKey);
		p.setPublicKey("*************");
		p.setPrivateKey("*************");
		p.setAppKey("******************");
		return RespResult.getInstance(RespCode.SUCCESS,p);
	}

}